<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Java Beans</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="jee tutorial, Java, web applications, Java Beans">
<meta name="description" content="This part of the Java EE 5 tutorials covers Java Beans.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href=".." title="Contents">Contents</a>




<h1>Java Beans</h1>

<p>
In this part of the JEE tutorials, we will talk about client side Java Beans 
components. This is a technology directly supported by the JavaServer pages. 
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 

<p>
Java Beans are reusable software components. The idea behind a software 
component is to create a specialized piece of self-contained code, that 
could be easily plugged into various applications as needed. For example in GUI
programming, we might have a chart, a clock or a spreadsheet component 
that could be used in an application without
exposing the programmer to complicated details that are behind the code.
</p>

<p>
 Technically Java Beans are Java classes conforming to particular
conventions. A bean can be a particular specialized Java Swing component 
(e.g. a chart ) that can be plugged into the application, a server side 
component called <b>Enterprise Java Bean</b>, EJB  or a client side component. In this
chapter, we will talk about client side Java Beans. 
</p>

<ul>
<li>The class must have a no-argument public constructor</li>
<li>The properties of the Bean must be accessible using accessor methods</li>
<li>The class should be serializable</li>
</ul>


<p>
JavaServer Pages technology directly supports using JavaBeans components
with standard JSP language elements. 
</p>

<h2>A Bean</h2>


<p>
In the Bean example we have a form that sends data to a jsp page. The JSP 
page will output those data. This time we do not use scriptlets or expressions, 
but we use Java Beans technology.
</p>

<div class="codehead">style.css</div>
<pre class="code">
* { font-size: 12px; font-family: Verdana }

input { border: 1px solid #ccc }
</pre>

<p>
This is a css file for the code example.
</p>


<div class="codehead">index.css</div>
<pre class="code">
&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Bean&lt;/title&gt;
&lt;link rel="stylesheet" href="style.css" type="text/css"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form action="show.jsp" method="post"&gt;
 
&lt;table&gt;

&lt;tr&gt;
&lt;td&gt;Author&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="author"&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Title&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="title"&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Available&lt;/td&gt;
&lt;td&gt;&lt;input type="checkbox" name="available" value="true"&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;/table&gt;

&lt;br&gt;
&lt;input type="submit" value="submit"&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html>
</pre>

<p>
Here we define a simple form. We have three input boxes. The author, 
title and the availability of the book.
The parameters are sent to the show.jsp page.
</p>

<div class="codehead">MyBean.java</div>
<pre class="code">
package com.zetcode;

import java.io.Serializable;


public class MyBean implements Serializable {

    private String author = "";
    private String title = "";
    private String available = ""; 


    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAvailable() {
        return available;
    }

    public void setAvailable(String available) {
        this.available = available;
    }
}
</pre>


<p>
This is our bean. It is a simple java class. Has no argument constructor. It implements 
the <code>Serializable</code> interface. We have three properties of a Bean. 
Each of the property names begins with small letter. Each of the accessor methods is 
public. The properties are private.
The accessor methods consists of two parts. The first part begins with get, 
set or is and the second part
is the name of the property with first letter capitalized.
</p>


<div class="codehead">show.jsp</div>
<pre class="code">
&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Show&lt;/title&gt;
&lt;link rel="stylesheet" href="style.css" type="text/css"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;jsp:useBean id="MyBean" class="com.zetcode.MyBean" scope="page"&gt;
&lt;jsp:setProperty name="MyBean" property="author" param="author" /&gt;
&lt;jsp:setProperty name="MyBean" property="title" param="title" /&gt;
&lt;jsp:setProperty name="MyBean" property="available" param="available" /&gt;
&lt;/jsp:useBean&gt;

&lt;jsp:getProperty name="MyBean" property="author"/&gt;&lt;br&gt;
&lt;jsp:getProperty name="MyBean" property="title"/&gt;&lt;br&gt;
&lt;jsp:getProperty name="MyBean" property="available"/&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>

<p>
The show.jsp page sets the parameters into the bean and prints them.
</p>


<pre class="explanation">
&lt;jsp:useBean id="MyBean" class="com.zetcode.MyBean" scope="page"&gt;
...
&lt;/jsp:useBean&gt;
</pre>

<p>
We use this element to declare that our JSP page will use a bean. The id 
parameter identifies the bean. The class parameter is a fully clasified classname. 
The scope parameters sets the bean validity for this page only.
</p>

<pre class="explanation">
&lt;jsp:getProperty name="MyBean" property="author"/&gt;&lt;br&gt;
</pre>

<p>
This element retrieves the author property from the bean.
</p>


<h2>Another Bean</h2>

<p>
Next we modify our previous example a bit.
</p>

<div class="codehead">show.jsp</div>
<pre class="code">
&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Show&lt;/title&gt;
&lt;link rel="stylesheet" href="style.css" type="text/css"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;jsp:useBean id="MyBean" class="com.zetcode.MyBean" scope="page"&gt;
&lt;jsp:setProperty name="MyBean" property="*"/&gt;
&lt;/jsp:useBean&gt;

&lt;jsp:getProperty name="MyBean" property="author"/&gt;&lt;br&gt;
&lt;jsp:getProperty name="MyBean" property="title"/&gt;&lt;br&gt;
&lt;jsp:getProperty name="MyBean" property="available"/&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>

<p>
We slightly change the show.jsp page.
</p>

<pre class="explanation">
&lt;jsp:setProperty name="MyBean" property="*"/&gt;
</pre>

<p>
This element will automatically fill the bean properties with the request 
parameters. This works only if the parameter names match the bean property names.
</p>

<p>
In this chapter we have briefly mentioned Java Beans.
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br> 


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified January 21, 2008  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>
